package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class GetLoseUser extends BaseRepair {

    public GetLoseUser(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLogin("2020-10-21","2020-10-22");
        listMap = getNoLogin(listMap,"2020-10-22","2020-10-23");
        DataMgr.addData(listMap);
        listMap = getLogin("2020-10-22","2020-10-23");
        listMap = getNoLogin(listMap,"2020-10-23","2020-10-24");
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLogin(String start,String end) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_l_login WHERE `time` > '"+start+"' AND `time` < '"+end+"' GROUP BY userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("流失时间",end);
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    private List<Map<String, String>> getNoLogin(List<Map<String, String>> userList,String start,String end) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_l_login WHERE userId = ? AND `time` > '"+start+"' AND `time` < '"+end+"'";
        try {
            for(Map<String,String> user : userList){
                call = getGameServer().getLogConnection().prepareCall(sql);
                call.setString(1, user.get("userId"));
                rs = call.executeQuery();
                if(!rs.next()){
                    UserInfo userInfo = getUserInfo(user.get("userId"));
                    user.put("openId",userInfo.getOpenId());
                    if(isGet102431Patron(user.get("userId"))){
                        user.put("102431","1");
                    }else{
                        user.put("102431","0");
                    }
                    dataList.add(user);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }

    private boolean isGet102431Patron(String userId){
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_patrons WHERE userId = ? AND patronsId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            call.setInt(2, 102431);
            rs = call.executeQuery();
            while (rs.next()) {
                return true;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return false;
    }

}
